package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.WfCombiCondition;
import com.yu.crm.domain.pojo.WfCombiInfo;
import com.yu.crm.domain.request.ConditionInfoConfReq;
import com.yu.crm.domain.response.ActionConfRep;
import com.yu.crm.mapper.WfCombiConditionMapper;
import com.yu.crm.service.WfCombiConditionService;
import com.yu.crm.service.WfCombiInfoService;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组合条件关联表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-10-08
 */
@Service
public class WfCombiConditionServiceImpl extends ServiceImpl<WfCombiConditionMapper, WfCombiCondition> implements WfCombiConditionService {

    @Autowired
    private WfCombiInfoService wfCombiInfoService;
    /**
     * 通过appId做删除
     * @param appId
     */
    @Override
    public void del(String appId) {
        QueryWrapper<WfCombiCondition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        baseMapper.delete(queryWrapper);
    }

    /**
     * 获取条件信息
     * @param combiInfoId 条件信息id
     * @param appId
     * @return
     */
    @Override
    public ActionConfRep getCombiCondition(Long combiInfoId, String appId) {
        ActionConfRep actionConfRep = new ActionConfRep();
        WfCombiInfo wfCombiInfo = wfCombiInfoService.getById(combiInfoId);
        if (DataHelpers.isNotEmpty(wfCombiInfo)) {
            actionConfRep.setConditionRelationValue(wfCombiInfo.getCombiName());
        }
        QueryWrapper<WfCombiCondition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        queryWrapper.eq("condition_id",combiInfoId);
        List<WfCombiCondition> wfCombiConditions = baseMapper.selectList(queryWrapper);
        List<ConditionInfoConfReq> list = new ArrayList<>();
        if (DataHelpers.isNotEmpty(wfCombiConditions) && wfCombiConditions.size()>0) {
            for (WfCombiCondition combiCondition:wfCombiConditions
                 ) {
                ConditionInfoConfReq confReq = new ConditionInfoConfReq();
                //条件枚举id
                confReq.setConditionEnumId(combiCondition.getCombiId());
                //条件组合id
                confReq.setCombiConditionId(combiCondition.getId());
                confReq.setConditionType(confReq.getConditionType());
                confReq.setConditionValue(confReq.getConditionValue());
                list.add(confReq);
            }
        }
        actionConfRep.setConditionInfoConfReqList(list);
        return actionConfRep;
    }
}
